<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.error.log' );

define("WURFL_DIR", 	dirname(__FILE__).DS.'wurflphp'.DS.'WURFL'.DS);
define("RESOURCES_DIR", dirname(__FILE__).DS.'wurflphp'.DS.'resources'.DS);
define("WURFL_CONFIG_FILE", RESOURCES_DIR . 'wurfl-config.xml');

require_once( WURFL_DIR. 'WURFLManagerProvider.php');
	
class plgSystemMobileTag extends JPlugin
{
	function plgSystemMobileTag(&$subject,$config)
	{
		parent::__construct($subject,$config);
	}

	function ConvertToBoolean($val)
	{
		if (strtolower(trim($val)) === 'false')
		{
			return false;
  		}
		else
		{
			return (boolean)$val;
		}
	}

	function CheckAgent()
	{
		//return true if the User Agent is a mobile device
		$wurflManager = WURFL_WURFLManagerProvider::getWURFLManager(WURFL_CONFIG_FILE);
		$requestingDevice = $wurflManager->getDeviceForHttpRequest($_SERVER);
		$is_wireless = plgSystemMobileTag::ConvertToBoolean( $requestingDevice->getCapability('is_wireless_device') );

		$log = &JLog::getInstance('plg_mobiletag.log.php');
		$ua = (isset($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : "not set";
		$log->addEntry(array('comment' => "UA:$ua is wireless: $is_wireless"));

		return $is_wireless;
	}

	function isValidTemplate( $template )
	{
		$file_path = JPATH_SITE . DS .'templates'. DS. $template . DS;
		if ( !file_exists( $file_path . DS . 'index.php' ) )
		{
			return false;
		}
		return true;
	}

	function onAfterInitialise()
	{
		global $mainframe;
		if($mainframe->isAdmin()) return;
		$plugin=&JPluginHelper::getPlugin('system','mobiletag');
		$myParams = new JParameter($plugin->params);
		$myParams->def( 'template', '' );

		$GLOBALS['wurlf_ismobile'] = plgSystemMobileTag::CheckAgent();
		$is_wireless = $GLOBALS['wurlf_ismobile'];
		if( TRUE == $is_wireless)
		{
			$template = $myParams->get( 'template' );
			$isValideTempalte = plgSystemMobileTag::isValidTemplate( $template ); 
			if( $isValideTempalte )
				$mainframe->setTemplate( $template );

			$log = &JLog::getInstance('plg_mobiletag.log.php');
			$log->addEntry(array('comment' => "try to change the template to $template; is valid template: $isValideTempalte"));
		}
	}
	function onAfterRender()
	{
		$body = JResponse::getBody();
		if( !$GLOBALS['wurlf_ismobile'] )
		{
			//remove the html between <mtag device="mobile">...</mtag>
			$body = preg_replace( '|(\<mtag[^\>]*?mobile[^\>]*?\>.*?\<\/mtag\>)|is', '', $body );

			//remove the <mtag ...> and </mtag> tags, since they are not html tags
			$body = preg_replace( '|(\<mtag[^\>]*?\>)|is', '', $body );
			$body = preg_replace( '|(\<\/mtag[^\>]*?\>)|is', '', $body );
			JResponse::setBody($body);
			//remove mobile tags and content;
			return;
		}

		//remove the html between <mtag device="desktop">...</mtag>
		$body = preg_replace( "|(\<mtag[^\>]*?desktop[^\>]*?\>.*?\<\/mtag\>)|is", '', $body );

		//remove the <mtag ...> and </mtag> tags, since they are not html tags
		$body = preg_replace( '|(\<mtag[^\>]*?\>)|is', '', $body );
		$body = preg_replace( '|(\<\/mtag[^\>]*?\>)|is', '', $body );

		JResponse::setBody($body);
	}
}
?>